package Test;

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ControlLayer.PersonCtr;
import ControlLayer.SaleCtr;
import DBLayer.DBSale;
import ModelLayer.Sale;

public class SaleCtrTest {
	private SaleCtr saleCtr;
	private PersonCtr personCtr;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		saleCtr = new SaleCtr();
		personCtr = new PersonCtr();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testInsertAndGetSale() {
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int saleId = 0;
		try {
			saleId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Sale sale = saleCtr.getSale(saleId);
		Assert.assertEquals(saleId, sale.getId());
		Assert.assertEquals(guestId, sale.getGuest().getID());
		Assert.assertEquals(employeeId, sale.getEmployee().getID());
		Assert.assertEquals(100.0, sale.getTotalPrice());
		Assert.assertEquals("SaleUnpaid", sale.getPayStatus().toString());
		Assert.assertEquals("31/01/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(sale.getCreateDate()));
		Assert.assertEquals("01/02/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(sale.getPayDate()));
		Assert.assertEquals("CreditCard", sale.getPayMethod().toString());
		saleCtr.deleteSale(saleId);
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);

	}

	@Test
	public void testUpdateSaleInfo() {
		int guestId1 = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int guestId2 = personCtr.createGuest("Jacob", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId1 = personCtr.createEmployee("Fay", "Stay",
				"SunnyRoad 4", "Morroco", "4563456", "stjy@gmail.com",
				"Employee", "23565589535", "Receptionist");
		int employeeId2 = personCtr.createEmployee("Nane", "Stay",
				"SunnyRoad 4", "Morroco", "4563456", "stjy@gmail.com",
				"Employee", "23565589535", "Receptionist");
		int saleId = 0;
		try {
			saleId = saleCtr.insertSale2(guestId1, employeeId1, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			saleCtr.updateSaleInfo(saleId, guestId2, employeeId2, 50.0,
					"SalePaid", "01/02/2013", "31/03/2013", "Cash", null, null);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Sale sale = saleCtr.getSale(saleId);
		Assert.assertEquals(saleId, sale.getId());
		Assert.assertEquals(guestId2, sale.getGuest().getID());
		Assert.assertEquals(employeeId2, sale.getEmployee().getID());
		Assert.assertEquals(50.0, sale.getTotalPrice());
		Assert.assertEquals("SalePaid", sale.getPayStatus().toString());
		Assert.assertEquals("01/02/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(sale.getCreateDate()));
		Assert.assertEquals("31/03/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(sale.getPayDate()));
		Assert.assertEquals("Cash", sale.getPayMethod().toString());
		saleCtr.deleteSale(saleId);
		personCtr.deleteGuest(guestId1);
		personCtr.deleteEmployee(employeeId1);
		personCtr.deleteGuest(guestId2);
		personCtr.deleteEmployee(employeeId2);
	}

	@Test
	public void testDeleteSale() {
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int saleId = 0;
		try {
			saleId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Assert.assertEquals(1, saleCtr.deleteSale(saleId));
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);
	}

}
